package com.zhao.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhao.common.support.Response;
import com.zhao.common.support.StatusCodeEnum;
import com.zhao.common.util.HttpClientUtil;
import com.zhao.data.vo.ApiStoreResult;

/**
 * 外部接口
 * 
 * @author zhaoyongfei
 *
 */
@Controller
@RequestMapping("open")
public class OpenApiController extends FrontBaseController {

	@Autowired
	HttpClientUtil httpClientUtil;

	@Autowired
	ObjectMapper objectMapper;

	@Autowired
	RestTemplate restTemplate;

	/**
	 * 图灵机器人
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("tuling")
	public @ResponseBody Response tuling(String info) {
		if (StringUtils.isEmpty(info)) {
			return Response.backResponse(StatusCodeEnum.EMPTY_PARAMETER, "参数不可为空");
		}
		String result = httpClientUtil.get("http://www.tuling123.com/openapi/api", new BasicNameValuePair("info", info), new BasicNameValuePair("key", "2b4d0321a2d3fdaafaaae885ad54f50b"));
		try {
			Map map = objectMapper.readValue(result, Map.class);
			return Response.backSuccess(map.get("text"));
		} catch (Exception e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.SYSTEM_EXCEPTION, "出错了");
		}
	}

	/**
	 * 手机号归属地查询
	 * 
	 * @param phoneNumber
	 * @return
	 */
	@RequestMapping("locate/mobilenumber")
	public @ResponseBody Response locateMobileNumber(String phoneNumber) {
		if (StringUtils.isEmpty(phoneNumber)) {
			return Response.backResponse(StatusCodeEnum.EMPTY_PARAMETER, "手机号不可为空");
		}
		return exec("http://apis.baidu.com/apistore/mobilenumber/mobilenumber", new BasicNameValuePair("phone", phoneNumber));
	}

	/**
	 * 查询支持天气服务的城市列表
	 * 
	 * @param cityname
	 * @return
	 */
	@RequestMapping("weatherservice/citylist")
	public @ResponseBody Response citylist(String cityname) {
		if (StringUtils.isEmpty(cityname)) {
			return Response.backResponse(StatusCodeEnum.EMPTY_PARAMETER, "城市名不可为空");
		}
		return exec("http://apis.baidu.com/apistore/weatherservice/citylist", new BasicNameValuePair("cityname", cityname));
	}

	public void test(String phoneNumber) {
		Map<String, Object> urlVariables = new HashMap<String, Object>();
		urlVariables.put("phone", phoneNumber);
		HttpHeaders headers = new HttpHeaders();
		headers.set("apiKey", "37c6b6d5154b5057fe861fb450afa9ae");
		HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
		ResponseEntity<String> result = restTemplate.exchange("http://apis.baidu.com/apistore/mobilenumber/mobilenumber", HttpMethod.GET, requestEntity, String.class, urlVariables);
		if (result.getStatusCode().equals(HttpStatus.OK)) {
			result.getBody();
		}
	}

	public Response exec(String uri, final NameValuePair... nvps) {
		try {
			String result = httpClientUtil.get(uri, nvps);
			ApiStoreResult returnJson = objectMapper.readValue(result, ApiStoreResult.class);
			if (returnJson == null) {
				return Response.backResponse(StatusCodeEnum.EMPTY_RESULT, "没有查询到结果");
			} else if (returnJson.getErrNum().equals(0)) {
				return Response.backSuccess(returnJson.getRetData());
			} else {
				return Response.backSuccess(returnJson);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.SYSTEM_EXCEPTION, "出错了");
		}
	}
}
